package test;

/**
 * @author 豆包LU
 * @date 2023/4/19 10:04
 */
import java.util.concurrent.TimeUnit;

public class BlockedThread {

    public static void main(String[] args) {
        Object lock = new Object();

        Thread thread1 = new Thread(() -> {
            synchronized (lock) {
                while (true) {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        Thread thread2 = new Thread(() -> {
            synchronized (lock) {
                System.out.println("Thread 2 acquired lock");
            }
        });

        thread1.start();
        try {
            TimeUnit.SECONDS.sleep(2); // 等待2秒，确保thread1已经进入循环并获取了锁
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread2.start();

        while (true) {
            Thread.State state1 = thread1.getState();
            Thread.State state2 = thread2.getState();
            System.out.println("Thread 1 state: " + state1);
            System.out.println("Thread 2 state: " + state2);
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
